package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthAgent;
import com.tmc.util.ObjectUtil;

@Repository
public class EthAgentDaoImpl extends BaseDaoImpl<EthAgent> {

	public Pagination<EthAgent> findByCon(ContextInfo context, EthAgent cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthAgent c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getAgentCode())) {
			sql.append(" and (c.agentCode = :agentCode or c.agentName like :agentCode) ");
			params.put("agentCode", "%" + cond.getAgentCode() + "%");
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<EthAgent> findAll(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthAgent c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}
	
	public EthAgent getByCode(ContextInfo context, String agentCode) {
		if (!ObjectUtil.hasText(agentCode)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthAgent c where c.enabledFlag = 'Y' ");
		sql.append(" and c.agentCode = :agentCode ");
		params.put("agentCode", agentCode);
		return this.get(sql.toString(), params);
	}

}
